
package com.benimetum.beans;

import com.benimetum.items.Forum;
import com.benimetum.items.Reply;
import com.benimetum.items.Topic;
import com.benimetum.managers.ForumManager;
import com.benimetum.managers.LoginManager;
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

/**
 *
 * @author Ethem Cem Ozkan
 * @email ethemcem.ozkan@gmail.com
 * No 07110604
 */
@ManagedBean(name="ForumBean")
@SessionScoped
public class ForumBean implements Serializable
{
    private Forum target;
    private Topic targetTopic;
    private Reply targetReply;
    private String text;
    private String header;
    private int activeTab;

    public ForumBean() {
    }

    public int getActiveTab() {
        return activeTab;
    }
    

    public Forum getTarget() {
	  return target;
    }

    public void setText(String text) {
	  this.text = text;
    }

    public String getText() {
	  return text;
    }

    public void setTarget(Forum target) {
	  this.target = target;
	  text="";
	  header="";
    }

    public void setTargetReply(Reply targetReply) {
	  this.targetReply = targetReply;
    }

    public void setActiveTab(int activeTab) {
        this.activeTab = activeTab;
    }

    public Reply getTargetReply() {
	  return targetReply;
    }

    public void setHeader(String header) {
	  this.header = header;
    }

    public String getHeader() {
	  return header;
    }

    public Topic getTargetTopic() {
	  return targetTopic;
    }

    public void setTargetTopic(Topic targetTopic) {
	  this.targetTopic = targetTopic;
	  text="";
	  header="RE:"+targetTopic.getHeader();
    }
    
    public boolean sameStarter(){
	  return targetTopic.getPerson().getId().equals(LoginManager.getUser().getId());
    }
    
    public boolean admin(){
	  return target.getCourse().getTEACHER().getId().equals(LoginManager.getUser().getId());
    }
    
    public void addReply(){
	  ForumManager.createReply(targetTopic.getId(), LoginManager.getUser().getId(), text, header);
	  targetTopic=ForumManager.getTopic(targetTopic.getId());
	  text="";
	  header="RE:"+targetTopic.getHeader();
    }
    
    public void addTopic(){
	  ForumManager.createTopic("f"+target.getCourse().getID(), LoginManager.getUser().getId(), text, header);
	  target=ForumManager.getForum("f"+target.getCourse().getID());
	  text="";
	  header="";
    }
    
    public void deleteReply(){
	  ForumManager.deleteReply(targetReply.getId());
	  targetTopic.setReplies(ForumManager.getTopicsRepliesArray(targetTopic.getId()));
    }
    
    public void deleteTopic(){
	  ForumManager.deleteTopic(targetTopic.getId());
	  targetTopic=null;
	  target=ForumManager.getForum("f"+target.getCourse().getID());
	  FacesContext context = FacesContext.getCurrentInstance();
	  HomePageBean obj = context.getApplication().evaluateExpressionGet(context, "#{HomePageBean}", HomePageBean.class);
	  obj.clickForum();
	  header="";
    }
    
    public void openTab(){
        activeTab=1;
    }
}
